package com.wan.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wan.common.utils.PageResult;
import com.wan.entity.Classes;
import com.wan.entity.Student;
import com.wan.mapper.StudentMapper;
import com.wan.service.IClassesService;
import com.wan.service.IStudentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wan.vo.StudentVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wan
 * @since 2025-01-07
 */
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements IStudentService {
    @Autowired
    private IClassesService classesService;
    @Override
    public PageResult<Student> findByPage(Integer page, Integer pageSize) {
        Page<Student> page1 = this.page(new Page<>(page, pageSize));
        return new PageResult<>(page1.getRecords(),page1.getTotal());
    }

    @Override
    public PageResult<Student> search(Integer page, Integer pageSize, StudentVO vo) {
        QueryWrapper<Student> wrapper = new QueryWrapper<>();
        if (StrUtil.isNotBlank(vo.getSname())) {
            wrapper.like("sname", vo.getSname());
        }
        if (StrUtil.isNotBlank(vo.getAddr())) {
            wrapper.like("addr", vo.getAddr());
        }
        if (vo.getCid() != null && vo.getCid() != 0) {
            wrapper.eq("cid", vo.getCid());
        }
        Page<Student> page1 = this.page(new Page<>(page, pageSize), wrapper);
        page1.getRecords().forEach(st -> {
            Classes classes = classesService.getById(st.getCid());
            st.setCname(classes.getCname());
        });
        return new PageResult<>(page1.getRecords(),page1.getTotal());
    }
    @Override
    public boolean add(Student student) {
        return this.save(student);
    }
}